<?php
//+----------------------------------------------------------------------
//| ThinkPHP [ WE CAN DO IT JUST THINK IT ]
//+----------------------------------------------------------------------
//| Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
//+----------------------------------------------------------------------
//| Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
//+----------------------------------------------------------------------
//| Author: liu21st <liu21st@gmail.com>
//+----------------------------------------------------------------------
//$Id: View.class.php 2702 2012-02-02 12:35:01Z liu21st $
/**
 +------------------------------------------------------------------------------
 * ThinkPHP 视图输出
 +------------------------------------------------------------------------------
 * @category   Think
 * @package  Think
 * @subpackage  Core
 * @author liu21st <liu21st@gmail.com>
 * @version  $Id: View.class.php 2702 2012-02-02 12:35:01Z liu21st $
 * @last modify 2012-12-05 16:20:43 by mashanling
 +------------------------------------------------------------------------------
 */
class View {
    private   $template_file;//模板文件
    protected $template_vars = array(); //模板输出变量
    public    $cache_id = null; //cache_id
    public    $compile_id = null; //cache_id
    public    $smarty = null;//smarty实例

    /**
     * 获取模板文件

     * @author mashanling
     *
     * @date        2012-12-05 16:32:53
     * @last modify 2012-12-05 16:32:56 by mashanling
     *
     * @access public
     *
     * @param string $template 模板文件
     *
     * @return string 模板文件
     */
    private function locationTemplate($template = '') {
        return $template ? $template : $this->template_file;
    }

    /**
     * 输出内容文本可以包括Html
     *
     * @last modify 2012-12-05 16:32:56 by mashanling
     *
     * @access public
     *
     * @param string $content      输出内容
     * @param string $charset      模板输出字符集
     * @param string $content_type 输出类型
     *
     * @return mixed 无返回值
     */
    private function show($content, $charset = '', $content_type = '') {
        echo $content;//输出模板文件
    }

    /**
     * 构造函数
     *
     * @author      mashanling
     *
     * @date        2012-12-06 09:53:30
     * @last modify 2012-12-06 09:53:30 by mashanling
     *
     * @access      public
     *
     * @param string $name  变量名
     * @param mixed  $value 变量值
     *
     * @return object 本类实例
     */
    public function __construct() {
        $this->template_file = THEME_PATH . MODULE_NAME . '/' . ACTION_NAME . C('TMPL_TEMPLATE_SUFFIX');

        $this->setTemplate();
    }

    /**
     * 模板变量赋值
     *
     * @last modify 2012-12-05 16:21:34 by mashanling
     *
     * @access public
     *
     * @param string $name  变量名
     * @param mixed  $value 变量值
     *
     * @return object 本类实例
     */
    public function assign($name, $value = '') {

        if (is_array($name)) {
            $this->template_vars = array_merge($this->template_vars, $name);
        }
        elseif (is_object($name)) {

            foreach ($name as $key => $val) {
                $this->template_vars[$key] = $val;
            }
        }
        else {
            $this->template_vars[$name] = $value;
        }

        return $this;
    }

    /**
     * 生成静态页
     *
     * @last modify 2012-12-04 18:06:11 by mashanling
     *
     * @access protected
     *
     * @param string $htmlfile 文件名称，不包括文件后缀
     * @param string $htmlpath 文件路径
     * @param string $template 模板文件,默认为空 由系统自动定位模板文件
     *
     * @return object 本类实例
     */
    public function buildHtml($htmlfile = '', $htmlpath = '', $template = '') {
        $content  = $this->fetch($template);
        $htmlpath = $htmlpath ? $htmlpath : HTML_PATH;
        $htmlfile = $htmlpath . $htmlfile . C('HTML_FILE_SUFFIX');


        !is_dir(dirname($htmlpath)) && mkdir($htmlpath, 0755, true);//如果静态目录不存在 则创建

        if (false === file_put_contents($htmlfile, $content)) {
            throw new Exception(L('_CACHE_WRITE_ERROR_') . ':' . $htmlfile);
        }

        return $this;
    }

    /**
     * 缓存渲染模板输出
     *
     * @last modify 2012-12-04 18:09:36 by mashanling
     *
     * @access protected
     *
     * @param string $template   模板文件,默认为空 由系统自动定位模板文件
     * @param string $cache_id   缓存id
     * @param string $compile_id 编译id
     *
     * @return mixed smarty->display()结果
     */
    public function cachedDisplay($template = '', $cache_id = null, $compile_id = null) {
        return $this->smarty->display($this->locationTemplate($template), $cache_id, $compile_id);
    }

    /**
     * 清除所有smarty 静态缓存
     *
     * @last modify 2012-12-04 18:11:43 by mashanling
     *
     * @access protected
     *
     * @param mixed $exp_time 过期时间
     *
     * @return mixed smarty->clearAllCache()结果
     */
    public function clearAllCache($exp_time = null) {
        return $this->smarty->clearAllCache($exp_time);
    }

    /**
     * 清除smarty指定模板缓存
     *
     * @last modify 2012-12-04 18:12:26 by mashanling
     *
     * @access protected
     *
     * @param string $template   模板文件,默认为空 由系统自动定位模板文件
     * @param string $cache_id   缓存id
     * @param string $compile_id 编译id
     * @param mixed  $exp_time   过期时间
     *
     * @return mixed smarty->clearCache()结果
     */
    public function clearCache($template = '', $cache_id = null, $compile_id = null, $exp_time = null) {
        return $this->smarty->clearCache($this->locationTemplate($template), $cache_id, $compile_id, $exp_time);
    }

    /**
     * 清除smarty编译文件
     *
     * @last modify 2012-12-04 18:12:26 by mashanling
     *
     * @access protected
     *
     * @param string $template 模板文件,默认为空 由系统自动定位模板文件
     *
     * @return mixed smarty->clearCompiledTemplate()结果
     */
    public function clearCompiledTemplate($template = null) {
        return $this->smarty->clearCompiledTemplate(null === $template ? null : $this->locationTemplate($template));
    }

    /**
     * 渲染输出
     *
     * @last modify 2012-12-05 16:24:35 by mashanling
     *
     * @access protected
     *
     * @param string $template_file 模板文件,默认为空 由系统自动定位模板文件
     * @param string $cache_id      缓存id
     * @param string $compile_id    编译id
     * @param string $charset       字符集
     * @param string $content_type  文档类型
     *
     * @return object 本类实例
     */
    public function display($template_file = '', $cache_id = null, $compile_id = null, $charset = '', $content_type = '') {
        $content = $this->fetch($this->locationTemplate($template_file), $cache_id, $compile_id);
        $this->show($content, $charset, $content_type);//输出模板内容

        return $this;
    }

    /**
     * 获取输出页面内容
     *
     * @last modify 2012-12-05 16:27:13 by mashanling
     *
     * @access public
     *
     * @param string $template_file 模板文件,默认为空 由系统自动定位模板文件
     * @param string $cache_id      缓存id
     * @param string $compile_id    编译id
     *
     * @return string 页面内容
     */
    public function fetch($template_file = '', $cache_id = null, $compile_id = null) {
        ob_start();//页面缓存
        $engine = strtolower(C('TMPL_ENGINE_TYPE'));

        if ('php' == $engine) {//使用PHP原生模板
            extract($this->template_vars, EXTR_OVERWRITE);//模板阵列变量分解成为独立变量
            include $template_file;//直接载入PHP模板
        }
        elseif ('smarty' == $engine) {//smarty模板
            return $this->smarty->assign($this->template_vars)->fetch($this->locationTemplate($template_file), $cache_id, $compile_id);
        }
        else {//视图解析标签
            $params = array('var' => $this->template_vars, 'file' => $template_file);
            tag('view_parse', $params);
        }

        $content = ob_get_clean();//获取并清空缓存

        return $content;//输出模板文件
    }//end fetch

    /**
     * 取得模板变量的值
     *
     * @last modify 2012-12-05 16:29:24 by mashanling
     *
     * @access public
     *
     * @param string $name 变量名
     *
     * @return mixed 变量值或空字符串
     */
    public function get($name) {
        return isset($this->template_vars[$name]) ? $this->template_vars[$name] : '';
    }

    /**
     * 取得模板所有变量
     *
     * @last 2012-12-05 16:30:00 by mashanling
     *
     * @access public
     *
     * @return array 所有变量
     */
    public function getAllVar() {
        return $this->template_vars;
    }

    /**
     * 判断模板是否缓存
     *
     * @last modify 2012-12-04 18:19:53 by mashanling
     *
     * @access protected
     *
     * @param string $template      模板文件,默认为空 由系统自动定位模板文件
     * @param string $cache_id      缓存id
     * @param string $compile_id    编译id
     *
     * @return bool 已缓存，返回true，否则返回false
     */
    public function isCached($template = '', $cache_id = null, $compile_id = null) {

        if (!$this->smarty || !$this->smarty->caching) {
            return false;
        }

        return $this->smarty->isCached($this->locationTemplate($template), $cache_id, $compile_id);
    }

    /**
     * 设置模板引擎
     *
     * @author      mashanling
     *
     * @date        2012-12-06 11:53:28
     * @last modify 2012-12-04 18:19:53 by mashanling
     *
     * @access public
     *
     * @return object 本类实例
     */
    public function setTemplate() {
        $engine = strtolower(C('TMPL_ENGINE_TYPE'));

        if (C('INIT_TEMPLATE') && 'smarty' == $engine) {
            require_cache(VENDOR_PATH . 'Smarty/Smarty.class.php');

            $this->smarty = new Smarty();
            $this->smarty->setTemplateDir(THEME_PATH);
            $this->smarty->setCompileDir(TEMP_PATH);
            $this->smarty->setCacheDir(CACHE_PATH);
            $this->smarty->setConfigDir(CONF_PATH);

            $config = C('TMPL_ENGINE_CONFIG');

            if ($config) {

                foreach ($config as $key => $val) {
                    $this->smarty->{$key} = $val;
                }
            }
        }

        return $this;
    }
}